package com.tpod.taxi.utils;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class SysUtils {

	public static boolean isInternetConected() {
		ConnectivityManager cm = (ConnectivityManager) UIUtils.context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnectedOrConnecting()) {
			return true;
		}
		return false;
	}

	public static String getLocation() throws IOException {
		UIUtils.checkInit();
		GPSTracker gps = new GPSTracker(UIUtils.context);
		Geocoder geocoder = new Geocoder(UIUtils.context, Locale.ENGLISH);
		String city = null;
		if (gps.canGetLocation()) {
			double latitude = gps.getLatitude();
			double longitude = gps.getLongitude();
			List<Address> addresses = geocoder.getFromLocation(latitude,
					longitude, 1);

			if (addresses != null) {
				Address returnedAddress = addresses.get(0);
				city = returnedAddress.getAddressLine(3);
			}

		}
		return city;
	}

}
